package accounts;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DeleteAccountServlet extends HttpServlet{

	private String accountKey;
	private String searhKeyWord;
	private static DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) 
			throws IOException {
		
		this.accountKey = req.getParameter("AccountKey");
		this.searhKeyWord = req.getParameter("keyword");
		Key entityKey = KeyFactory.stringToKey(this.accountKey);
		
		try {
			Entity UserAccount = datastore.get(entityKey);
			// delete the entity in the datastore
			datastore.delete(UserAccount.getKey());
		} catch (EntityNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(this.searhKeyWord.equals("all")){
			resp.sendRedirect("/admin_viewAllUsers.jsp");
		} else {
			resp.sendRedirect("/admin_userSearchResults.jsp?&keyword="+searhKeyWord);
		}
	}
}
